/**
 * 
 */
package com.wm.service.imp;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;

import com.sina.sae.storage.SaeStorage;
import com.sina.sae.util.SaeUserInfo;
import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.Restaurant;
import com.wm.bean.RestaurantTime;
import com.wm.dao.AreaDAO;
import com.wm.dao.CityDAO;
import com.wm.dao.RestaurantDAO;
import com.wm.dao.RestaurantTimeDAO;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.RestaurantService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;

/**
 * @author zhangbaochao
 *
 */
public class RestaurantServiceImp implements RestaurantService {
	
	private RestaurantDAO restaurantDAO;
	private RestaurantTimeDAO restaurantTimeDAO;

	public RestaurantTimeDAO getRestaurantTimeDAO() {
		return restaurantTimeDAO;
	}

	public void setRestaurantTimeDAO(RestaurantTimeDAO restaurantTimeDAO) {
		this.restaurantTimeDAO = restaurantTimeDAO;
	}

	public RestaurantDAO getRestaurantDAO() {
		return restaurantDAO;
	}

	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}
	
	/**
	 * 保存Restaurant对象
	 * 
	 * @param restaurant
	 * 			Restaurant
	 * @return Restaurant
	 * 			Restaurant
	 */
	public Restaurant saveRestaurant(Restaurant restaurant){
		Restaurant r = this.getRestaurantDAO().saveRestaurant(restaurant);
		String basePath =  SaeUserInfo.getSaeTmpPath() + "/";
		SaeStorage ss = new SaeStorage();//使用Storage存储
		//使用upload方法上传到域为image下
		ss.upload("image", basePath+r.getLogo(), r.getId() + ".jpg");
		return r;
	}
	
	/**
	 * 更新Restaurant对象
	 * 
	 * @param restaurant
	 * 			Restaurant
	 * @return Restaurant
	 * 			Restaurant
	 */
	public Restaurant updateRestaurant(Restaurant restaurant){
		Restaurant r = this.getRestaurantDAO().updateRestaurant(restaurant);
		if(StringUtils.isNotBlank(r.getLogo())){
			String basePath =  SaeUserInfo.getSaeTmpPath() + "/";
			SaeStorage ss = new SaeStorage();//使用Storage存储
			//使用upload方法上传到域为image下
			ss.upload("image", basePath+r.getLogo(), r.getId() + ".jpg");
		}
		return r;
	}
	
	/**
	 * 根据ID获取Restaurant对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeRestaurantById(long id){
		this.getRestaurantDAO().removeRestaurantById(id);
	}
	
	/**
	 * 根据ID获取Restaurant对象
	 * 
	 * @param id
	 * 			long
	 * @return Restaurant
	 * 			Restaurant
	 */
	public Restaurant findRestaurantById(long id){
		return this.getRestaurantDAO().findRestaurantById(id);
	}
	
	/**
	 * 根据参数获取Restaurant对象列表
	 * 
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @return PageList
	 * 			PageList
	 */
	public PageList findRestaurantsByResNameValidIsShow(String resName,int valid,int isShow,String orderby,int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getRestaurantDAO().getRestaurantNumByResNameValidIsShow(resName,valid,isShow));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
        List l = this.getRestaurantDAO().findRestaurantsByResNameValidIsShow(resName,valid,isShow,orderby, ascOrDesc, pages.getSpage(), pages.getPerPageNum());
        if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		} 
	}
	
	/**
	 * 前台查询用
	 * @param id
	 * @param valid
	 * @param isShow
	 * @return
	 */
	public Restaurant findRestaurantByIdValidIsShow(long id,int valid,int isShow){
		Restaurant r = this.getRestaurantDAO().findRestaurantByIdValidIsShow(id, valid, isShow);
		if(r!=null){
			r.setTimeList(this.getRestaurantTimeDAO().findRestaurantTimesByResIdIsShow(id, 1, "startHour", 0));
			r.setIsBusiness(this.compareTime(r.getTimeList()));
		}
		return r;
	}
	
	public static int compareTime(List timeList){
		int r=0;
		if(timeList!=null){ 
			//设置时区
			Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT+8")); 
			int hours = c.get(Calendar.HOUR_OF_DAY); 
			int minutes = c.get(Calendar.MINUTE);
			for(int i=0;i<timeList.size();i++){
				RestaurantTime rt = (RestaurantTime)timeList.get(i);
				if(hours > rt.getStartHour() && hours < rt.getEndHour()){
					r = 1;
					break;
				}else if(hours == rt.getStartHour() && hours < rt.getEndHour()){
					r = 1;
					break;
				}else if(hours == rt.getEndHour() && minutes < rt.getEndMinute()){
					r = 1;
					break;
				}
			}
			//System.out.println(r+"  "+hours+"  "+minutes);
		}
		return r;
	}
	
	/**
	 * 根据坐标查询
	 * @param pointX
	 * @param pointY
	 * @param valid
	 * @param isShow
	 * @return
	 */
	public List findRestaurantByPointXPointYValidIsShow(double pointX,double pointY,int valid,int isShow ){
		return this.getRestaurantDAO().findRestaurantByPointXPointYValidIsShow(pointX, pointY, valid, isShow);
	}
}
